package com.sgb.kjwl.view.ui.activity.self.publish;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.View;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPersonPublishNextBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.SuccessEntity;
import com.sgb.kjwl.model.entity.user.AddTalentEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.self.PersonnelPublishEvent;
import com.sgb.kjwl.view.ui.activity.self.authentication.CommitSuccessActivity;
import com.sgb.kjwl.view.ui.adapter.self.publish.ImageAdapter;
import com.sgb.kjwl.viewmodel.self.publish.PersonnelNextModel;
import com.swgk.core.base.di.AppComponent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.inject.Inject;

import static com.sgb.kjwl.view.event.SuccessType.SUCC_PERSON;

public class PersonPublishNextActivity extends AppActivity {
    private ActivityPersonPublishNextBinding binding;
    private ImageAdapter adapter;
    private List<String> urls;
    private List<File> fileList;
    private SuccessEntity dates;
    @Inject
    PersonnelNextModel viewModel;

    public static void start(Context context) {
        start(context, null);
    }

    public static void start(Context context, Intent extras) {
        Intent intent = new Intent();
        intent.setClass(context, PersonPublishNextActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (extras != null) {
            intent.putExtras(extras);
        }
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }


    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(PersonPublishNextActivity.this, R.layout.activity_person_publish_next);
        binding.setViewModel(viewModel);
        binding.backView.setOnClickListener(this);
        binding.nextView.setOnClickListener(this);
    }

    @Override
    protected void init() {
        urls = new ArrayList<>();
        fileList = new ArrayList<>();
        adapter = new ImageAdapter(this, null, null, 5);
        binding.recyclerView.setLayoutManager(new GridLayoutManager(PersonPublishNextActivity.this, 4));
        binding.recyclerView.setAdapter(adapter);
    }

    public void showSuccess() {
        dates = new SuccessEntity();
        dates.setType(SUCC_PERSON);
        dates.setTitle("发布成功");
        dates.setButtonText("确定");
        dates.setContent("您发布信息已提交成功，请前往我的人才页面查看");
        CommitSuccessActivity.start(this, dates);
    }

    public void uploadImgSuccess(List<String> urls, List<String> netUrls) {
        List<String> urlList = new ArrayList<>();
        urlList.addAll(netUrls);
        urlList.addAll(urls);
        Objects.requireNonNull(viewModel.addTalentEntity.get()).setImgList(urlList);
        if (Objects.requireNonNull(viewModel.addTalentEntity.get()).getId() != null) {
            viewModel.editPersonnelModel();
        } else {
            viewModel.addPersonnelModel();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {// 图片结果回调
                List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                fileList.clear();
                urls.clear();
                for (LocalMedia localMedia : selectList) {
                    File file = new File(localMedia.getRealPath());
                    fileList.add(file);
                    urls.add(localMedia.getRealPath());
                }
                adapter.setDatas(selectList, urls);
                adapter.setFileList(fileList);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_view:
                finish();
                break;
            case R.id.next_view:
                if (adapter.getFileList().size() != 0) {
                    viewModel.uploadMutiFileModel(adapter.getFileList(), adapter.getNetUrls());
                } else {
                    if (Objects.requireNonNull(viewModel.addTalentEntity.get()).getId() != null) {
                        viewModel.editPersonnelModel();
                    } else {
                        viewModel.addPersonnelModel();
                    }
                }
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(PersonnelPublishEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_PUBLISH_PERSON_NEXT) {
            AddTalentEntity addTalentEntity = (AddTalentEntity) event.getMessageData();
            viewModel.addTalentEntity.set(addTalentEntity);
            adapter.setNetImgList(addTalentEntity.getImgList());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}

